package com.itheima.tanhua.controller;

import com.itheima.tanhua.service.CommentService;
import com.itheima.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Lff
 * @date 2022/3/28
 */
@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发表评论
     */
    @PostMapping
    public ResponseEntity publishComment(@RequestBody Map map) {
        //获取参数
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentService.publishComment(movementId, comment);
        return ResponseEntity.ok(null);
    }

    @GetMapping
    public ResponseEntity getComments(@RequestParam(value = "movementId", required = true) String movementId,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        PageResult pr = commentService.getComments(movementId, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 点赞评论
     *
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity doLikeComment(@PathVariable("id") String commentId) {
        Integer count = commentService.doLikeComment(commentId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞评论
     *
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity doDisLikeComment(@PathVariable("id") String commentId) {
        Integer count = commentService.doDisLikeComment(commentId);
        return ResponseEntity.ok(count);
    }
}
